
//
//  BDHomeCellImageview.m
//  BadBoy
//
//  Created by wen on 2017/3/13.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDHomeCellImageview.h"

@interface BDHomeCellImageview ()

@end

@implementation BDHomeCellImageview

- (void)configeWithModel:(id)model{
    for (UIView *subView in self.subviews) {
        [subView removeFromSuperview];
    }
    
    self.homeModel = model;
    if (self.homeModel.images ==nil) {
        return;
    }
    
    if (self.homeModel.images.count == 1) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 200, 150)];
//        // BDLog(ASLocalizedString(@"这个url是啥:%@"), self.homeModel.images.firstObject);
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.homeModel.images.firstObject] placeholderImage:[UIImage imageNamed:@"business_pic"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            if (CGSizeEqualToSize(CGSizeZero, image.size)) {
                return;
            }
            imageView.width = 150.0 / image.size.height * image.size.width;
            if (imageView.width > kBDScreenWidth - 20) {
                imageView.width = kBDScreenWidth - 20;
                imageView.height = image.size.height * imageView.width / 1.0 / image.size.width;
                
            }
        }];
        [self addSubview:imageView];
        imageView.userInteractionEnabled = YES;
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [imageView addGestureRecognizer:tapGR];
    }else{
        CGFloat side = 10;
        CGFloat space = 5;
        CGFloat photoWidth = ([UIScreen mainScreen].bounds.size.width - space * 2 - side * 2) / 3.0;
        for (int i = 0; i < self.homeModel.images.count; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(side + (space + photoWidth) * (i % 3), (space + photoWidth) * (i / 3), photoWidth, photoWidth)];
//            imageView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.homeModel.images[i]] placeholderImage:[UIImage imageNamed:@"business_pic"]];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.clipsToBounds = YES;
            [self addSubview:imageView];
            imageView.tag = 3321 + i;
            imageView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
            [imageView addGestureRecognizer:tapGR];
        }
    }
}

- (void)tapAction:(UITapGestureRecognizer *)tapGR{
    UIView *view = tapGR.view;
    if (self.imageClickBlock) {
        self.imageClickBlock(view.tag - 3321, self.homeModel.images, view);
    }
//    UIWindow *keywind = [UIApplication sharedApplication].keyWindow;
//    CGRect rect = [self convertRect:view.frame toView:keywind];
//    // BDLog(ASLocalizedString(@"转换过的坐标：%@"),NSStringFromCGRect(rect));
//    BigImageView *bigImageView = [[BigImageView alloc] init];
//    bigImageView.startRect = rect;
//    bigImageView.startIndex = view.tag - 3321;
//    bigImageView.imageArray = self.homeModel.images;
//    [keywind addSubview:bigImageView];
//    [bigImageView show];
}

@end
